package com.deepgames.mud.action.book.core;

import java.util.HashMap;
import java.util.Map;

import com.deepgames.mud.action.data.Action;
import com.deepgames.mud.action.data.ActionContext;
import com.deepgames.mud.core.InternalCode;

public class Login implements Action {

	private static final String welcomeMessage =
		"Welcome to DeepMUD Pre-Testing Build! \n" +
		"This is an unversioned test.\n\n" +
		"If you have a name registered, enter it at the prompt." +
		"If you do not have a name registered, enter \"NEW\" at the " +
		"prompt.\n\n";
	private static final String loginPrompt =
		"LOGIN> ";
	private static final String passPrompt =
		"PASSWORD> ";
	
	private Map<String,String> userStatus = new HashMap<String,String>();
	private Map<String,String> userNames = new HashMap<String,String>();
	
	@Override
	public int execute(String sourceID, String args, ActionContext context) {
		int status = InternalCode.SUCCESS;
		
		/*
		 * Permissions check.
		 */
		if (!sourceID.equals("SYSTEM")) {
			status = InternalCode.ACT_NO_PERM;
		}
		
		if (status == InternalCode.SUCCESS) {
			String[] parsedArgs = args.trim().split(" ");
			String userID = parsedArgs[0].trim();
			String curUserStat = userStatus.get(userID);
			
			/*
			 * No status: User has just logged in.
			 */
			if (curUserStat == null) {
				context.sendMsg(userID, welcomeMessage);
				context.sendMsg(userID, loginPrompt);
				userStatus.put(userID, "PROMPT_NAME");
			}
			
			/*
			 * Prompted for name: User has the login prompt.
			 */
			else if (curUserStat.equals("PROMPT_NAME")) {
				/*
				 * If the user sends nothing then re-send the login prompt and do not change status.
				 */
				if (parsedArgs.length < 2 || parsedArgs[1].trim().equals("")) {
					context.sendMsg(userID, loginPrompt);
				}
				/*
				 * If the user enters "NEW" then pass through information to NEW_PLAYER.
				 */
				else if (parsedArgs[1].trim().toUpperCase().equals("NEW")) {
					userStatus.put(userID, "NEW_PLAYER");
					context.lookupAction("NEW_PLAYER").execute("SYSTEM", args, context);
				}
				/*
				 * Otherwise assume the user has sent their name and needs to be prompted for their password.
				 */
				else {
					context.sendMsg(userID, passPrompt);
					userStatus.put(userID, "PROMPT_PASS");
					userNames.put(userID, parsedArgs[1].trim());
				}
			}
			
			/*
			 * New player pass-through: Allow "NEW_PLAYER" to handle messages.
			 */
			else if (curUserStat.equals("NEW_PLAYER")) {
				context.lookupAction("NEW_PLAYER").execute("SYSTEM", args, context);
			}
			
			/*
			 * Prompted for password: Attempt to authenticate.
			 */
			else if (curUserStat.equals("PROMPT_PASS")) {
				/*
				 * TODO: Perform user authentication.
				 * 	If it fails, re-prompt for password.
				 * 	If it succeeds:
				 * 	1. Assign returned player ID to session.
				 *  2. Call INSTANCE_PLAYER.
				 *  3. Call SEND_PLAYER.
				 */
				
				context.sendMsg(userID, "Username is " + curUserStat);
				context.sendMsg(userID, "Password is " + parsedArgs[1]);
			}
		}
		
		return status;
	}

}
